<template>
  <div class="viewcontainer" v-if="dataset">
    <el-dialog
      title="初始化库存"
      :visible.sync="detailFormVisible"
      width="500px"
      :close-on-click-modal="false"
    >
      <hi-form
        ref="initForm"
        v-if="detailFormVisible"
        :model="initFormData"
        :dataSource="dataset"
        v-bind="initForm"
      ></hi-form>
      <!-- <initStock></initStock> -->
      <span slot="footer" class="dialog-footer">
        <!-- <el-button @click="detailFormVisible = false">取 消</el-button> -->
        <el-button type="primary" @click="cilicOk">确定</el-button>
      </span>
    </el-dialog>

    <hi-toolbar :source="dataset" v-bind="toolbar"></hi-toolbar>
    <el-button type="primary" @click="initStock">初始化库存</el-button>
    <hi-form
      ref="detailForm"
      :model="dataset.curRecord"
      :dataSource="dataset"
      v-bind="form"
    ></hi-form>
    <!-- <div style="height: 400px">
      <hi-editor></hi-editor>
      <button>清空内容</button>
      <button>禁用</button>
    </div> -->
  </div>
</template>

<script>
import { HiForm, HiToolbar, HiEditor, utils } from "hi-ui";
import viewBase from "../../base";
import datasetCfg from "./dataset";
import storeCfg from "./drugDatabase";
import brandCfg from "../brand/dataset";
import store from "@/api/store";
export default {
  name: "drugdetail",
  extends: viewBase,
  components: {
    HiForm,
    HiToolbar,
    HiEditor,
  },
  data() {
    let dataset = null;
    return {
      FENTERPRISEID: null,
      // 初始化库存数据
      initFormData: {
        FDESC: null,
        FBARCODE: null,
        FAMOUNT: null,
        FSHOPDESC: null,
        PRODUCTIONDATE: null,
        EXPIRATIONDATE: null,
      },
      //数据集配置
      datasetCfg: datasetCfg,
      //数据集
      dataset: dataset,
      detailFormVisible: false,
      toolbar: {
        buttons: [
          {
            kind: "save",
            //text: "保存",
            type: "danger",
            icon: "el-icon-upload",
          },
        ],
      },
      //数据桥梁
      dataBridge: new Map(),
      //编辑数据
      editData: {},
      //初始化库存表单配置
      initForm: {
        columnsTotal: 1,
        items: [
          {
            name: "FDESC",
            props: {
              disabled: true,
            },
          },
          {
            name: "FBARCODE",
            props: {
              disabled: true,
            },
          },
          {
            name: "FAMOUNT",
            props: {
              min: 0,
              precision: 2,
            },
          },
          {
            name: "FSHOPDESC",
            element: "hi-select-grid",
            required: true,
            props: {
              selectOption: {
                loading: true,
                multiple: false,
                clearable: true,
                filterable: true,
                "allow-create": true,
                "collapse-tags": true,
              },
              lookup: {
                modelFile: "database/shop/dataset/ShopEntity.xml",
                label: "FDESC",

                tableprops: {
                  "show-header": false,
                  "max-height": 400,
                },
                returnFields: {
                  FSHOPDESC: "FDESC",
                  FSHOPDESCID: "FNUMBER",
                },
                columns: [
                  {
                    name: "FDESC",
                    label: "通用名称",
                  },
                ],
              },
            },
          },
          {
            element: "el-date-picker",
            name: "PRODUCTIONDATE",
          },
          {
            element: "el-date-picker",
            name: "EXPIRATIONDATE",
          },
        ],
      },
      //表单配置
      form: {
        columnsTotal: 3,
        colPercents: [1, 1, 1],
        labelWidth: "180px",
        items: [
          {
            name: "FNUMBER",
          },
          {
            name: "FDESC",
            element: "hi-data-select",
            props: {
              lookup: {
                storeCfg: storeCfg,
                active: true,
                /* treeOption:{
                                                                    "modelFile":"users",
                                                                    label: "name",
                                                                    children: "company"
                                                                }, */
                multiSelect: false,
                returnFields: {
                  FAPPROVALNUMBER: "an",
                  FDESC: "name",
                  FDOSAGEFORM: "dsform",
                  FSPEC: "spec",
                  FPROADDRESS: "addr",
                  FPROCOMPANY: "unit",
                  FCLASS: "cat",
                  FAPPROVALDATE: "adate",
                },
                columns: [
                  {
                    name: "name",
                    label: "产品通用名称",
                  },

                  {
                    name: "spec",
                    label: "规格",
                  },
                  {
                    name: "dsform",
                    label: "剂型",
                  },
                  {
                    name: "unit",
                    label: "生产单位",
                  },
                  {
                    name: "addr",
                    label: "生产地址",
                  },
                  {
                    name: "cat",
                    label: "产品类型",
                  },
                  {
                    name: "an",
                    label: "批准文号",
                  },
                  {
                    name: "adate",
                    label: "批准日期",
                  },
                ],
                queryOption: {
                  fields: [
                    {
                      name: "name",
                      label: "产品通用名称",
                      element: "el-input",
                    },
                    {
                      name: "unit",
                      label: "生产单位",
                      element: "el-input",
                    },
                    {
                      name: "an",
                      label: "批准文号",
                      element: "el-input",
                    },
                  ],
                },
              },
            },
          },
          {
            name: "FBARCODE",
          },
          {
            name: "FBRAND",
            element: "hi-data-select",
            props: {
              lookup: {
                storeCfg: brandCfg,
                active: true,
                /* treeOption:{
                                                                    "modelFile":"users",
                                                                    label: "name",
                                                                    children: "company"
                                                                }, */
                clearable: true,
                multiSelect: false,
                returnFields: {
                  FBRAND: "FDESC",
                },
                columns: [
                  {
                    width: "200px",
                    name: "FDESC",
                    label: "品牌",
                  },

                  {
                    name: "FNOTE",
                    label: "备注",
                  },
                ],
                queryOption: {
                  fields: [
                    {
                      name: "name",
                      label: "产品通用名称",
                      element: "el-input",
                    },
                  ],
                },
              },
            },
          },
          {
            name: "FSPEC",
          },
          {
            name: "FUNIT",
            element: "hi-select-grid",
            props: {
              selectOption: {
                // remote:true,
                loading: true,
                multiple: false,
                clearable: true,
                filterable: true,
                "allow-create": true,
                "collapse-tags": true,
              },
              lookup: {
                modelFile: "database/base/dataset/BaseEntity.xml",

                param: {
                  FPID: "jiliangdawi",
                },
                tableprops: {
                  // border:true,
                  //stripe:true,
                  "show-header": false,
                  //fit:false,
                  size: "mini",
                  "max-height": 400,
                },
                returnFields: {
                  FUNIT: "FDESC",
                },
                columns: [
                  {
                    name: "FDESC",
                    label: "单位",
                  },
                ],
              },
            },
          },

          {
            name: "FCLASS",
          },
          /*  {
           label:"成本价"
            name: "FCOSTPRICE",
            props: {
              format: "￥0.00",
              precision: 2,
            },
          }, */
          /*     {
            label:"进价"
            name: "FPURCHASINGPRICE",
            props: {
              format: "￥0.00",
              precision: 2,
            },
          }, */
          {
            name: "FPROCOMPANY",
          },
          {
            name: "FDOSAGEFORM",
          },
          {
            name: "FPROADDRESS",
          },

          {
            name: "FPRICE",
            props: {
              min: 0,
              format: "￥0.00",
              precision: 2,
            },
          },
          {
            name: "FVIPPRICE",
            props: {
              min: 0,
              format: "￥0.00",
              precision: 2,
            },
          },
          {
            name: "FENTERRATE",
            props: {
              min: 0,
              precision: 2,
            },
          },
          {
            name: "FCANCELRATE",
            props: {
              min: 0,
              precision: 2,
            },
          },
          // {
          //   name: "FSTOREUPPER",
          //   props: {
          //     min: 0,
          //   },
          // },
          {
            name: "FAPPROVALNUMBER",
          },
          {
            name: "FAPPROVALDATE",
          },
          // {
          //   name: "FSTORELOWER",
          //   props: {
          //     min: 0,
          //   },
          // },

          {
            name: "FBUSINESSSCOPE",
            element: "hi-select-grid",
            props: {
              selectOption: {
                // remote:true,
                loading: true,
                multiple: false,
                clearable: true,
                filterable: true,
                "allow-create": true,
                "collapse-tags": true,
              },
              lookup: {
                modelFile: "database/base/dataset/BaseEntity.xml",
                param: {
                  FPID: "jyfwfl",
                },
                tableprops: {
                  // border:true,
                  //stripe:true,
                  "show-header": false,
                  //fit:false,
                  size: "mini",
                  "max-height": 400,
                },
                returnFields: {
                  FBUSINESSSCOPE: "FDESC",
                  FBUSINESSSCOPEID: "FVALUE",
                },
                columns: [
                  {
                    name: "FDESC",
                    label: "药品类别",
                  },
                ],
              },
            },
          },

          {
            name: "FPRESCRICLASS",
          },
          {
            name: "FMEDICARETYPE",
            element: "hi-select-grid",
            props: {
              selectOption: {
                // remote:true,
                loading: true,
                multiple: false,
                clearable: true,
                filterable: true,
                "allow-create": true,
                "collapse-tags": true,
              },
              lookup: {
                modelFile: "database/base/dataset/BaseEntity.xml",
                param: {
                  FPID: "ybyylx",
                },
                tableprops: {
                  // border:true,
                  //stripe:true,
                  "show-header": false,
                  //fit:false,
                  size: "mini",
                  "max-height": 400,
                },
                returnFields: {
                  FMEDICARETYPE: "FDESC",
                  FMEDICARETYPEID: "FVALUE",
                },
                columns: [
                  {
                    name: "FDESC",
                    label: "医保类别",
                  },
                ],
              },
            },
          },

          {
            name: "FDRUGCLASS",
            element: "hi-select-grid",
            props: {
              selectOption: {
                // remote:true,
                loading: true,
                multiple: false,
                clearable: true,
                filterable: true,
                "allow-create": true,
                "collapse-tags": true,
              },
              lookup: {
                modelFile: "database/base/dataset/BaseEntity.xml",
                param: {
                  FPID: "ypfl",
                },
                tableprops: {
                  // border:true,
                  //stripe:true,
                  "show-header": false,
                  //fit:false,
                  size: "mini",
                  "max-height": 400,
                },
                returnFields: {
                  FDRUGCLASS: "FDESC",
                  FDRUGCLASSID: "FVALUE",
                },
                columns: [
                  {
                    name: "FDESC",
                    label: "药品分类",
                  },
                ],
              },
            },
          },
          {
            name: "FSTORECOND",
            element: "hi-select-grid",
            props: {
              selectOption: {
                // remote:true,
                loading: true,
                multiple: false,
                clearable: true,
                filterable: true,
                "allow-create": true,
                "collapse-tags": true,
              },
              lookup: {
                modelFile: "database/base/dataset/BaseEntity.xml",
                param: {
                  FPID: "cctj",
                },
                tableprops: {
                  // border:true,
                  //stripe:true,
                  "show-header": false,
                  //fit:false,
                  size: "mini",
                  "max-height": 400,
                },
                returnFields: {
                  FSTORECOND: "FDESC",
                  FSTORECONDID: "FVALUE",
                },
                columns: [
                  {
                    name: "FDESC",
                    label: "储存条件",
                  },
                ],
              },
            },
          },

          {
            name: "FABC",
            element: "hi-select-grid",
            props: {
              selectOption: {
                // remote:true,
                loading: true,
                multiple: false,
                clearable: true,
                filterable: true,
                "allow-create": true,
                "collapse-tags": true,
              },
              lookup: {
                modelFile: "database/base/dataset/BaseEntity.xml",
                param: {
                  FPID: "abcj",
                },
                tableprops: {
                  // border:true,
                  //stripe:true,
                  "show-header": false,
                  //fit:false,
                  size: "mini",
                  "max-height": 400,
                },
                returnFields: {
                  FABC: "FDESC",
                  FABCID: "FVALUE",
                },
                columns: [
                  {
                    name: "FDESC",
                    label: "ABC级",
                  },
                ],
              },
            },
          },
          {
            name: "FISIMPORT",
          },
          {
            name: "FISRETAIL",
          },
          {
            name: "FNOTE",
            colspan: 3,
            props: {
              type: "textarea",
              autosize: {
                minRows: 4,
                maxRows: 4,
              },
              maxlength: 200,
            },
          },
          // {
          //   name: "FUSERID",
          // },
          // {
          //   name: "FCREATEDATE",
          // },
          {
            name: "FIMG",
            label: "",
            colspan: 3,
            element: "hi-upload",
            props: {
              hideUpload: true,
              "with-credentials": true,
              accept: "image/png, image/jpeg",
              limit: 5,
              // "show-file-list":false,
              // disabled:true,
              multiple: true,
              "list-type": "picture-card",
            },
          },
          {
            name: "FDETAIL",
            colspan: 3,
            element: "hi-editor",
            props: {},
            // colspan: 3,
            // props: {
            //   type: "textarea",
            //   autosize: {
            //     minRows: 4,
            //     maxRows: 4,
            //   },
            //   maxlength: 200,
            // },
          },
        ],
      },
      randomNum: "00" + (Math.random() + "").split(".")[1].substr(0, 8),
    };
  },
  model: {
    prop: "value",
    event: "valuechange",
  },
  props: {
    value: {
      type: Object,
    },
  },

  computed: {},
  watch: {},
  methods: {
    //打开格式化库存
    initStock() {
      this.detailFormVisible = true;
      this.initFormData.FDESC = this.dataset.curRecord.FDESC;
      this.initFormData.FBARCODE = this.dataset.curRecord.FBARCODE;
    },
    // 判断保存
    cilicOk() {
      if (this.initFormData.FAMOUNT === null) {
        this.$message.error("请选择数量");
        return;
      }
      if (this.initFormData.FSHOPDESC === null) {
        this.$message.error("请选择店铺");
        return;
      }
      if (this.initFormData.PRODUCTIONDATE === null) {
        this.$message.error("请选择生产日期");
        return;
      }
      if (this.initFormData.EXPIRATIONDATE === null) {
        this.$message.error("请选择有效期至");
        return;
      }
      this.$confirm(
        `初始化后该药品的数量将重置为【${this.initFormData.FAMOUNT}】件，确定要初始化吗?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        //格式化日期
        let PRODUCTIONDATE = utils.date.format(
          this.initFormData.PRODUCTIONDATE,
          "yyyy-MM-dd HH:mm:ss"
        );
        let EXPIRATIONDATE = utils.date.format(
          this.initFormData.EXPIRATIONDATE,
          "yyyy-MM-dd HH:mm:ss"
        );
        // 保存初始化库存
        store
          .initStoreSave({
            FAMOUNT: this.initFormData.FAMOUNT,
            FSHOPDESC: this.initFormData.FSHOPDESC,
            FSHOPDESCID: this.initFormData.FSHOPDESCID,
            PRODUCTIONDATE: PRODUCTIONDATE,
            EXPIRATIONDATE: EXPIRATIONDATE,
            FBARCODE: JSON.stringify([this.initFormData.FBARCODE]),
            FENTERPRISEID: this.FENTERPRISEID,
          })
          .then((res) => {
            this.$message({
              type: "success",
              message: "成功!",
            });
            this.detailFormVisible = false;
            this.initFormData.FAMOUNT = null;
            this.initFormData.PRODUCTIONDATE = null;
            this.initFormData.EXPIRATIONDATE = null;
          });
      });
    },
  },
  mounted() {
    this.dataset = this.createDataSet(this.datasetCfg);
    // 用户信息
    let userInfo = this.userInfo;
    this.FENTERPRISEID = userInfo.enterpriseid;
    //判断是否有门店
    if (userInfo.shopid) {
      this.initFormData.FSHOPDESC = userInfo.shopname;
      this.initFormData.FSHOPDESCID = userInfo.shopid;
      // this.initForm.items
      this.initForm.items.forEach((element) => {
        if (element.name == "FSHOPDESC") {
          element.props["disabled"] = true;
        }
      });
    }
    // this.dataset.on("afterload", (response, data) => {
    //   debugger;
    //   // this.initForm
    // });
    //关闭页面
    this.dataset.on("aftersave", (res, store) => {
      if (res.status == 200) {
        this.closepage();
      }
    });
    this.dataset.on("beforesave", (store) => {
      if (store.data.length == 0) return;
      let currNumber = store.data[0].FNUMBER;
      if (!currNumber) return;
      //店内编码
      if (
        currNumber.substr(2, 8) != this.randomNum.substr(2, 8) ||
        currNumber.length != this.randomNum.length
      ) {
        this.$message.error("店内编码只能修改前两位！");
        store.data[0].FNUMBER = this.randomNum;
        store.cancel = true;
      }
    });
    this.dataBridge.set(this.dataset, ["detailForm", "initializeForm"]);
    if (this.$route.query.id) {
      this.dataset
        .load({
          FID: this.$route.query.id,
        })
        .then((res) => {
          this.randomNum = this.dataset.data[0].FNUMBER;
        });
    } else {
      this.dataset.add({ FNUMBER: this.randomNum });
    }
  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scope>
.hi-toolbar {
  display: inline-block;
}
</style>
